<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="exporter-version" content="Evernote Mac 9.4.3 (461087)"/><meta name="altitude" content="7.080429553985596"/><meta name="author" content="704206198@qq.com"/><meta name="created" content="2020-08-16 14:04:46 +0000"/><meta name="latitude" content="31.37545776367188"/><meta name="longitude" content="121.493541683152"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2020-09-24 03:44:29 +0000"/><meta name="content-class" content="yinxiang.markdown"/><title>day06：后台管理项目的接口定义</title></head><body><div style="font-size: 14px; margin: 0; padding: 0; width: 100%;"><h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000; margin-top: 14px;">一、准备商品数据接口路由</h1>
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;">
<p style="line-height: 160%; box-sizing: content-box; color: #333; margin: 0;">在项目中创建新文件夹，准备存放接口文件：myapp----&gt;api文件夹</p>
<ol style="line-height: 160%; box-sizing: content-box; display: block; padding-left: 30px; margin: 6px 0 10px; color: #333; list-style-type: decimal; margin-top: 0; margin-bottom: 0;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;">
<p style="line-height: 160%; box-sizing: content-box; color: #333; margin: 0;">创建商品接口的路由文件：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">api----&gt;goods.js</code></p>
</li>
<li style="line-height: 160%; box-sizing: content-box; position: relative;">
<p style="line-height: 160%; box-sizing: content-box; color: #333; margin: 0;">在app.js中注册接口：</p>
<pre style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; margin: 2px 0 8px; background-color: #f5f7f8;"><code style="display: block; overflow-x: auto; background: #1e1e1e; line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; letter-spacing: -.3px; padding: 18px; color: #f4f4f4; white-space: pre-wrap;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">var</span> apiGoods = <span style="color: #4ec9b0; line-height: 160%; box-sizing: content-box;">require</span>(<span style="color: #d69d85; line-height: 160%; box-sizing: content-box;">"./api/goods.js"</span>);
app.use(<span style="color: #d69d85; line-height: 160%; box-sizing: content-box;">"/api/goods"</span>,apiGoods);
</code></pre>
</li>
<li style="line-height: 160%; box-sizing: content-box; position: relative;">
<p style="line-height: 160%; box-sizing: content-box; color: #333; margin: 0;">测试接口：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">localhost:3000/api/goods</code></p>
</li>
</ol>
</li>
</ul>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">二、实现商品列表接口数据</h1>
<ol style="line-height: 160%; box-sizing: content-box; display: block; padding-left: 30px; margin: 6px 0 10px; color: #333; list-style-type: decimal;">
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">引入数据库相关模块</p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">接收到路由请求后，读取数据库数据，处理之后返回</p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">数据格式规范：</p>
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333; margin-top: 0; margin-bottom: 0;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;">没有数据
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333; margin-top: 0; margin-bottom: 0;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;"><code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">{ "code": 400, "title": "请求失败", "msg": "NOT DATA" }</code></li>
</ul>
</li>
<li style="line-height: 160%; box-sizing: content-box; position: relative;">有数据
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333; margin-top: 0; margin-bottom: 0;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;"><code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">{ "code": 200, "title": "请求成功", "msg": 数据 }</code></li>
</ul>
</li>
</ul>
</li>
</ol>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">三、编写接口文档</h1>
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;">使用自动生成在线接口文档的工具：apidoc</li>
</ul>
<ol style="line-height: 160%; box-sizing: content-box; display: block; padding-left: 30px; margin: 6px 0 10px; color: #333; list-style-type: decimal;">
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">下载：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">npm i apidoc -g</code></p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">创建文档目录：在public文件夹内，创建apidoc文件夹</p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">配置package文件，增加：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">"apidoc": {"title":"接口文档", "url":"http://localhost:3000"}</code></p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">编写文档：在<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">api----&gt;goods.js</code>内，使用/***/的形式，添加注释，以注释的形式编写文档</p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">文档格式要求：</p>
</li>
</ol>
<pre style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; margin: 2px 0 8px; background-color: #f5f7f8;"><code style="display: block; overflow-x: auto; background: #1e1e1e; line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; letter-spacing: -.3px; padding: 18px; color: #f4f4f4; white-space: pre-wrap;"><span style="color: #57a64a; font-style: italic; line-height: 160%; box-sizing: content-box;">/**
* @apiGroup 商品信息
* @api {get} /api/goods 获取商品数据
* @apiVersion 0.0.0
* @apiDescription 分页请求商品数据
* @apiParam {Number} limit=10 数据条数
* @apiParam {Number} index=0 分页数据页码
* @apiSuccess {number} code 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} msg 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "code":200,
*    "title":"请求成功",
*    "msg":[{},{},...]
* }
* @apiError {number} code 请求状态
* @apiError {string} title 请求说明
* @apiError {array} msg 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":400,
*    "title":"请求失败",
*    "msg":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3000/api/goods
*/</span>
</code></pre>
<ol start="6" style="line-height: 160%; box-sizing: content-box; display: block; padding-left: 30px; margin: 6px 0 10px; color: #333; list-style-type: decimal;">
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">命令行运行：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">apidoc -i api/ -o public/apidoc</code></p>
</li>
<li style="line-height: 160%; box-sizing: content-box;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">启动项目后，浏览器查看：<code style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; color: #c1788b; padding: 4px 4px 2px 0; letter-spacing: -.3px;">localhost:3000/apidoc/index.html</code></p>
</li>
</ol>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">四、创建排序数据接口，生成接口文档</h1>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">五、创建分页数据接口，生成接口文档</h1>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">六、创建类目数据接口，生成接口文档</h1>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">七、创建商品详情数据接口，生成接口文档</h1>
<h1 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 41px; border-bottom: 3px double #999; color: #000;">八、总结，作业</h1>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">课外阅读<a href="https://www.jianshu.com/p/c7b9994792de" style="line-height: 160%; box-sizing: content-box; text-decoration: underline; color: #5286bc;">RestfulAPI规范</a></p>
</div><center style="display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden">%23%20%E4%B8%80%E3%80%81%E5%87%86%E5%A4%87%E5%95%86%E5%93%81%E6%95%B0%E6%8D%AE%E6%8E%A5%E5%8F%A3%E8%B7%AF%E7%94%B1%0A-%20%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%88%9B%E5%BB%BA%E6%96%B0%E6%96%87%E4%BB%B6%E5%A4%B9%EF%BC%8C%E5%87%86%E5%A4%87%E5%AD%98%E6%94%BE%E6%8E%A5%E5%8F%A3%E6%96%87%E4%BB%B6%EF%BC%9Amyapp----%3Eapi%E6%96%87%E4%BB%B6%E5%A4%B9%0A%20%20%20%20%0A%20%20%20%201.%20%E5%88%9B%E5%BB%BA%E5%95%86%E5%93%81%E6%8E%A5%E5%8F%A3%E7%9A%84%E8%B7%AF%E7%94%B1%E6%96%87%E4%BB%B6%EF%BC%9A%60api----%3Egoods.js%60%0A%20%20%20%20%0A%20%20%20%202.%20%E5%9C%A8app.js%E4%B8%AD%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3%EF%BC%9A%0A%20%20%20%20%20%20%20%20%60%60%60js%0A%20%20%20%20%20%20%20%20var%20apiGoods%20%3D%20require(%22.%2Fapi%2Fgoods.js%22)%3B%0A%20%20%20%20%20%20%20%20app.use(%22%2Fapi%2Fgoods%22%2CapiGoods)%3B%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%203.%20%E6%B5%8B%E8%AF%95%E6%8E%A5%E5%8F%A3%EF%BC%9A%60localhost%3A3000%2Fapi%2Fgoods%60%0A%0A%23%20%E4%BA%8C%E3%80%81%E5%AE%9E%E7%8E%B0%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8%E6%8E%A5%E5%8F%A3%E6%95%B0%E6%8D%AE%0A1.%20%E5%BC%95%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%B8%E5%85%B3%E6%A8%A1%E5%9D%97%0A%0A2.%20%E6%8E%A5%E6%94%B6%E5%88%B0%E8%B7%AF%E7%94%B1%E8%AF%B7%E6%B1%82%E5%90%8E%EF%BC%8C%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE%E5%BA%93%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%A4%84%E7%90%86%E4%B9%8B%E5%90%8E%E8%BF%94%E5%9B%9E%0A%0A3.%20%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F%E8%A7%84%E8%8C%83%EF%BC%9A%0A%20%20%20%20-%20%E6%B2%A1%E6%9C%89%E6%95%B0%E6%8D%AE%0A%20%20%20%20%20%20%20%20-%20%60%7B%20%22code%22%3A%20400%2C%20%22title%22%3A%20%22%E8%AF%B7%E6%B1%82%E5%A4%B1%E8%B4%A5%22%2C%20%22msg%22%3A%20%22NOT%20DATA%22%20%7D%60%0A%20%20%20%20-%20%E6%9C%89%E6%95%B0%E6%8D%AE%0A%20%20%20%20%20%20%20%20-%20%60%7B%20%22code%22%3A%20200%2C%20%22title%22%3A%20%22%E8%AF%B7%E6%B1%82%E6%88%90%E5%8A%9F%22%2C%20%22msg%22%3A%20%E6%95%B0%E6%8D%AE%20%7D%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%23%20%E4%B8%89%E3%80%81%E7%BC%96%E5%86%99%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%0A-%20%E4%BD%BF%E7%94%A8%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%9C%A8%E7%BA%BF%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%E7%9A%84%E5%B7%A5%E5%85%B7%EF%BC%9Aapidoc%0A%0A1.%20%E4%B8%8B%E8%BD%BD%EF%BC%9A%60npm%20i%20apidoc%20-g%60%0A%0A2.%20%E5%88%9B%E5%BB%BA%E6%96%87%E6%A1%A3%E7%9B%AE%E5%BD%95%EF%BC%9A%E5%9C%A8public%E6%96%87%E4%BB%B6%E5%A4%B9%E5%86%85%EF%BC%8C%E5%88%9B%E5%BB%BAapidoc%E6%96%87%E4%BB%B6%E5%A4%B9%0A%0A3.%20%E9%85%8D%E7%BD%AEpackage%E6%96%87%E4%BB%B6%EF%BC%8C%E5%A2%9E%E5%8A%A0%EF%BC%9A%60%22apidoc%22%3A%20%7B%22title%22%3A%22%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%22%2C%20%22url%22%3A%22http%3A%2F%2Flocalhost%3A3000%22%7D%60%0A%0A4.%20%E7%BC%96%E5%86%99%E6%96%87%E6%A1%A3%EF%BC%9A%E5%9C%A8%60api----%3Egoods.js%60%E5%86%85%EF%BC%8C%E4%BD%BF%E7%94%A8%2F***%2F%E7%9A%84%E5%BD%A2%E5%BC%8F%EF%BC%8C%E6%B7%BB%E5%8A%A0%E6%B3%A8%E9%87%8A%EF%BC%8C%E4%BB%A5%E6%B3%A8%E9%87%8A%E7%9A%84%E5%BD%A2%E5%BC%8F%E7%BC%96%E5%86%99%E6%96%87%E6%A1%A3%0A%0A5.%20%E6%96%87%E6%A1%A3%E6%A0%BC%E5%BC%8F%E8%A6%81%E6%B1%82%EF%BC%9A%0A%60%60%60js%0A%2F**%0A*%20%40apiGroup%20%E5%95%86%E5%93%81%E4%BF%A1%E6%81%AF%0A*%20%40api%20%7Bget%7D%20%2Fapi%2Fgoods%20%E8%8E%B7%E5%8F%96%E5%95%86%E5%93%81%E6%95%B0%E6%8D%AE%0A*%20%40apiVersion%200.0.0%0A*%20%40apiDescription%20%E5%88%86%E9%A1%B5%E8%AF%B7%E6%B1%82%E5%95%86%E5%93%81%E6%95%B0%E6%8D%AE%0A*%20%40apiParam%20%7BNumber%7D%20limit%3D10%20%E6%95%B0%E6%8D%AE%E6%9D%A1%E6%95%B0%0A*%20%40apiParam%20%7BNumber%7D%20index%3D0%20%E5%88%86%E9%A1%B5%E6%95%B0%E6%8D%AE%E9%A1%B5%E7%A0%81%0A*%20%40apiSuccess%20%7Bnumber%7D%20code%20%E8%AF%B7%E6%B1%82%E7%8A%B6%E6%80%81%0A*%20%40apiSuccess%20%7Bstring%7D%20title%20%E8%AF%B7%E6%B1%82%E8%AF%B4%E6%98%8E%0A*%20%40apiSuccess%20%7Barray%7D%20msg%20%E8%AF%B7%E6%B1%82%E6%95%B0%E6%8D%AE%0A*%20%40apiSuccessExample%20%7Bjson%7D%20Success-Response%3A%0A*%20%7B%0A*%20%20%20%20%22code%22%3A200%2C%0A*%20%20%20%20%22title%22%3A%22%E8%AF%B7%E6%B1%82%E6%88%90%E5%8A%9F%22%2C%0A*%20%20%20%20%22msg%22%3A%5B%7B%7D%2C%7B%7D%2C...%5D%0A*%20%7D%0A*%20%40apiError%20%7Bnumber%7D%20code%20%E8%AF%B7%E6%B1%82%E7%8A%B6%E6%80%81%0A*%20%40apiError%20%7Bstring%7D%20title%20%E8%AF%B7%E6%B1%82%E8%AF%B4%E6%98%8E%0A*%20%40apiError%20%7Barray%7D%20msg%20%E8%AF%B7%E6%B1%82%E6%95%B0%E6%8D%AE%0A*%20%40apiErrorExample%20%7Bjson%7D%20Error-Response%3A%0A*%20%7B%0A*%20%20%20%20%22code%22%3A400%2C%0A*%20%20%20%20%22title%22%3A%22%E8%AF%B7%E6%B1%82%E5%A4%B1%E8%B4%A5%22%2C%0A*%20%20%20%20%22msg%22%3A%22NOT%20DATA%22%0A*%20%7D%0A*%20%40apiSampleRequest%20http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fgoods%0A*%2F%0A%60%60%60%0A%0A6.%20%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%BF%90%E8%A1%8C%EF%BC%9A%60apidoc%20-i%20api%2F%20-o%20public%2Fapidoc%60%0A%0A7.%20%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE%E5%90%8E%EF%BC%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E6%9F%A5%E7%9C%8B%EF%BC%9A%60localhost%3A3000%2Fapidoc%2Findex.html%60%0A%0A%0A%23%20%E5%9B%9B%E3%80%81%E5%88%9B%E5%BB%BA%E6%8E%92%E5%BA%8F%E6%95%B0%E6%8D%AE%E6%8E%A5%E5%8F%A3%EF%BC%8C%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%0A%0A%23%20%E4%BA%94%E3%80%81%E5%88%9B%E5%BB%BA%E5%88%86%E9%A1%B5%E6%95%B0%E6%8D%AE%E6%8E%A5%E5%8F%A3%EF%BC%8C%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%0A%0A%23%20%E5%85%AD%E3%80%81%E5%88%9B%E5%BB%BA%E7%B1%BB%E7%9B%AE%E6%95%B0%E6%8D%AE%E6%8E%A5%E5%8F%A3%EF%BC%8C%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%0A%0A%23%20%E4%B8%83%E3%80%81%E5%88%9B%E5%BB%BA%E5%95%86%E5%93%81%E8%AF%A6%E6%83%85%E6%95%B0%E6%8D%AE%E6%8E%A5%E5%8F%A3%EF%BC%8C%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3%0A%0A%23%20%E5%85%AB%E3%80%81%E6%80%BB%E7%BB%93%EF%BC%8C%E4%BD%9C%E4%B8%9A%0A%0A%E8%AF%BE%E5%A4%96%E9%98%85%E8%AF%BB%5BRestfulAPI%E8%A7%84%E8%8C%83%5D(https%3A%2F%2Fwww.jianshu.com%2Fp%2Fc7b9994792de)%0A</center></body></html>